$(function () {
    var userUrl = "/oa/t-user";
    var diaryUrl = "/oa/t-diary";
    var token = sessionStorage.getItem('token');
    var oprateFormatter = function (value, row, index) {
        return [
            '<button id="tb_view" class="btn btn-default btn-xs" style="margin: 0 5px 0 5px">查看</button>',
            '<button id="tb_edit" class="btn btn-default btn-xs" style="margin: 0 5px 0 5px">编辑</button>',
            '<button id="tb_submit" class="btn btn-default btn-xs" style="margin: 0 5px 0 5px">提交</button>',
            '<button id="tb_delete" class="btn btn-default btn-xs" style="margin: 0 5px 0 5px">删除</button>',
        ].join("");
    };

    var oprateEvents = function(context) {
        var me = context;
        return {
            "click #tb_view" : function(e, value, row, index) {
                me.view([row]);
            },

            "click #tb_edit" : function(e, value, row, index) {
                me.edit([row]);
            },

            "click #tb_submit" : function(e, value, row, index) {
                me.state([row]);
            },

            "click #tb_delete" : function(e, value, row, index) {
                me.remove([row]);
            }
        };
    };

    var stateFormatter = function (value, row, index) {
        if (row.userState == 0) {
            return "停用";
        }else{
            return "启用"
        }
    };

    var teacherList = [];
    var teacherFormatter = function (value, row, index) {
        var teacherData = teacherList;
        for (var i = 0; i < teacherData.length; i++) {
            var teacher = teacherData[i];
            if (row.teacherId == teacher.id) {
                return teacher.userName;
            }
        }

        return row.teacherId;
    };

    var teacherLoader = function (me, field) {
        $(field).empty();
        var elem = $(field);
        var dataList = teacherList;
        elem.append('<option value="">请选择角色...</option>');
        for (var i = 0; i < dataList.length; i++) {
            var data = dataList[i];
            elem.append(
                '<option value="'
                + data.id
                + '">'
                + data.userName
                + '</option>'
            );
        }
    };

    var page = $.fn.cookePageTable({
        URL: diaryUrl,
        dialog: {
            domid: '#addDiaryModal',
            title: '日记',
            validate : {
                diaryTitle: {
                    required: true,
                },
                teacherId: {
                    required: true,
                    minlength: 1,
                    maxlength: 128,
                },
            },

            fieldLoader: {
                teacherId : teacherLoader,
            },
        },

        toolbar: {
            add: '#addBtnModel',
            del: '#batchDeletion',
            domid: '#toolbar',
            //search: '#search',
        },

        table: {
            domid: "#table",
            uniqueId: "diaryId", //每一行的唯一标识，一般为主键列
            uniqdesc: function(row) {
                return row.diaryTitle;
            },                  //每一行的唯一标识描述字段信息
            columns: [{
                checkbox: true
            }, {
                field: 'diaryTitle',
                title: '日记标题',
                filter: true,
            }, {
                field: 'createTime',
                title: '创建时间',
            }, {
                field: 'teacherId',
                title: '审阅老师',
                formatter: teacherFormatter,
            }, {
                field: 'updateTime',
                title: '更新时间',
            }, {
                field: 'diaryState',
                title: '状态',
                formatter: stateFormatter,
            }, {
                field: 'fraction',
                title: '成绩',
            }, {
                field: 'remarks',
                title: '建议',
            }, {
                field: 'operate',
                title: '操作',
                visible: true,
                events: oprateEvents,
                formatter: oprateFormatter,
            }]
        },
    });

    page.view = function (arrselections) {
        var me = this;
        if (arrselections.length <= 0) {
            $.message({type:'warning', message:'请勾选要编辑的效数据'});
            return;
        }

        if (arrselections.length > 1) {
            $.message({type:'warning', message:'只能勾选一行进行编辑'});
            return;
        }

        var data = arrselections[0];
        $('#diaryTitle2').html(data.diaryTitle);
        $('#diaryText2').html(decodeURIComponent(data.diaryText));
        dialog = $('#seeModal');
        dialog.modal();
    };

    page.listTeacher = function (successFun) {
        var me = this;
        $.ajax({
            url: userUrl + '/listTeacher',
            type: "GET",
            dataType: "json",
            headers: {Token: token},
            success: function (data) {
                if (data.code != 200){
                    $.message({
                        message : data.msg || data.error,
                        type:'error'
                    });
                    return;
                }

                if (successFun) {
                    successFun(me, data.data);
                }
            },
        });
    }; // end of page.resetPwd

    page.listTeacher(function (me, data) {
        teacherList = data;
        me.refresh();
    });
});